

type ThrottleFunction = (...args: any[]) => any;
/**
 * 节流
 */
export function throttle(func: ThrottleFunction, delay: number = 300): ThrottleFunction {
  let timer: ReturnType<typeof setTimeout> | null = null;

  return (...args: any[]): void => {
    // @ts-ignore
    const context = this;

    if (!timer) {
      timer = setTimeout(function () {
        func.apply(context, args);
        timer = null;
      }, delay);
    }
  };
}

type DebounceFunction = (...args: any[]) => any;

/**
 * 防抖
 */
export function debounce(func: DebounceFunction, delay: number): DebounceFunction {
  let timer: ReturnType<typeof setTimeout> | null = null;

  return (...args: any[]): void => {
    // @ts-ignore
    const context = this;

    if (!timer) {
      timer = setTimeout(function () {
        func.apply(context, args);
        timer = null;
      }, delay);
    }
  };
}
